package net.hubt.itc.util;

import net.hubt.itc.ui.R;
import net.hubt.itc.ui.utils.CTable;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

public class LayoutUtils {
	public static android.view.ViewGroup.LayoutParams makeLinearLayoutParamFillWidth() {
		return new android.view.ViewGroup.LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
	}

	public static android.view.ViewGroup.LayoutParams makeLinearLayoutParamFillAll() {
		return new android.view.ViewGroup.LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT);
	}

	/**
	 * create a vertical linear layout contains:
	 * 		+ a header (non-header if tableHeaderText is null)
	 * 		+ a scroll bar contains :
	 * 			-a table
	 * @param ctx
	 * @param table
	 * @param tableHeaderText
	 * @return
	 */
	public static LinearLayout createLinearScrollableTableLayout(Context ctx,
			CTable table, String tableHeaderText) {
		LinearLayout mainLayout = new LinearLayout(ctx);
		android.widget.LinearLayout.LayoutParams mainLayoutParams = new android.widget.LinearLayout.LayoutParams(
				android.widget.LinearLayout.LayoutParams.FILL_PARENT,
				android.widget.LinearLayout.LayoutParams.FILL_PARENT);
		mainLayout.setLayoutParams(mainLayoutParams);		
		mainLayout.setOrientation(LinearLayout.VERTICAL);
		
		ScrollView scroller = new ScrollView(ctx);
		scroller.addView(table.getLayout());
		
		//do not create header for the table
		if (tableHeaderText != null)
			mainLayout.addView(makeHeading(ctx, tableHeaderText));
		mainLayout.addView(scroller);
		return mainLayout;
	}
	
	/**
	 * create scrollable table and a table as a table header
	 * @param ctx
	 * @param table a scrollable table
	 * @param tableHeader a table as a table header
	 * @return
	 */
	public static LinearLayout createLinearScrollableTableLayout(Context ctx,
			CTable table, CTable tableHeader) {
		LinearLayout mainLayout = new LinearLayout(ctx);
		android.widget.LinearLayout.LayoutParams mainLayoutParams = new android.widget.LinearLayout.LayoutParams(
				android.widget.LinearLayout.LayoutParams.FILL_PARENT,
				android.widget.LinearLayout.LayoutParams.FILL_PARENT);
		mainLayout.setLayoutParams(mainLayoutParams);		
		mainLayout.setOrientation(LinearLayout.VERTICAL);
		
		ScrollView scroller = new ScrollView(ctx);
		scroller.addView(table.getLayout());
		
		//header is a table
		mainLayout.addView(tableHeader.getLayout());
		//content is a table		
		mainLayout.addView(scroller);
		return mainLayout;
	}

	/**
	 * make text view header for table
	 * @param ctx
	 * @param headerText
	 * @return
	 */
	public static TextView makeHeading(Context ctx, String headerText) {
		/*
		TextView header = new TextView(ctx);
		header.setBackgroundResource(R.drawable.blue_theme_textview);
		header.setText(headerText);
//		header.setTextSize(17f);
//		header.setPadding(10, 10, 10, 10);
		header.setGravity(Gravity.LEFT);
//		header.setBackgroundColor(Color.BLACK);
		//header.setBackgroundColor(Color.parseColor("#323232"));
		 * */
		 
		return makeHeading(ctx, R.drawable.blue_theme_textview,Typeface.DEFAULT_BOLD,headerText);
	}
	
	
	public static TextView makeHeading(Context ctx,int themeResourceId,Typeface tf, String headerText){
		LayoutParams lpTv = new LayoutParams(
				android.widget.LinearLayout.LayoutParams.FILL_PARENT,
				android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
		TextView tv = new TextView(ctx);		
		//have to make sure this text view will fill width and height
		tv.setLayoutParams(lpTv);
		tv.setText(headerText);
		tv.setTypeface(tf);
		// a <selector> xml file placed in <drawable> dir
		tv.setBackgroundResource(themeResourceId);
		// effective alignment
		tv.setGravity(Gravity.CENTER);
		tv.setTextColor(CTable.DEFAULT_TEXT_COLOR);
		tv.setShadowLayer(.5f, 1, 1, CTable.DEFAULT_TEXT_SHADOW_COLOR);
		return tv;
	}
}
